package tuling.netty.splitpacket.encoding;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import tuling.netty.splitpacket.domain.RgsMessageProtocol;

/**
 * @author RenGaoshuai
 * @date 2023/12/4 14:53
 * @description 自定义编码器，要实现messageToByteEncoder
 * 其他格式转成netty的byteBuf对象
 */
public class RgsCustomizeEncode extends MessageToByteEncoder<RgsMessageProtocol> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, RgsMessageProtocol msg, ByteBuf out) throws Exception {
        System.out.println("自定义编码器的 encode 方法被调用");
        out.writeInt(msg.getLen());
        out.writeBytes(msg.getContent());
    }
}
